<?php

class Session
{
    const ADMIN_KEY = 'loginAdmin';
    const USER_KEY = 'loginUser';
    /**
     * 开启session
     */
    public static function start()
    {
        // 如果session没有开启，开启session
        if (session_status() !== PHP_SESSION_ACTIVE) {
            session_start();
        }
    }

    /**
     * 设置session
     * @param $key
     * @param $value
     */
    public static function set($key, $value)
    {
        self::start();
        $_SESSION[$key] = $value;
    }

    /**
     * 获取session
     * @param $key
     * @return mixed|null
     */

    public static function get($key)
    {
        self::start();
        if (isset($_SESSION[$key])) {
            return $_SESSION[$key];
        } else {
            return null;
        }
    }

    /**
     * 删除session
     * @param $key
     */

    public static function del($key)
    {
        self::start();
        unset($_SESSION[$key]);
    }

    /**
     * 清空session
     */
    public static function clear()
    {
        self::start();
        session_unset();
    }



    /**
     * 是否登录用户
     * @return bool
     */
    public static function isLoginUser(): bool
    {
        self::start();
        return isset($_SESSION[self::USER_KEY]);
    }

    /**
     * 是否登录管理员
     * @return bool
     */
    public static function isLoginAdmin(): bool
    {
        self::start();
        return isset($_SESSION[self::ADMIN_KEY]);
    }

    /**
     * 设置登录的用户
     * @param $user
     */

    public static function setUser($user)
    {
        self::start();
        self::set(self::USER_KEY, $user);
    }
    /**
     * 设置登录的管理员
     * @param $admin
     */
    public static function setAdmin($admin)
    {
        self::start();
        self::set(self::ADMIN_KEY, $admin);
    }

    /**
     * 获取登录的用户
     * @return mixed|null
     */
    public static function getUser()
    {
        self::start();
        return self::get('loginUser');
    }

    /**
     * 获取登录的管理员
     * @return mixed|null
     */

    public static function getAdmin()
    {
        self::start();
        return self::get('loginAdmin');
    }
}